package com.l.web.master.system.extension;

import org.l.qiao.Cluster;
import org.l.qiao.Page;
import org.l.qiao.builder.Field;
import org.l.qiao.builder.Query;
import org.l.qiao.web.KeyUpdate;
import com.l.web.common.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ExtensionService {

    @Autowired
    private ExtensionDAO extensionDAO;

    public ExtensionService() {
    }

    public List<Cluster> getPageList(Page page) {
        Query query = new Query();
        Field field = new Field();
        field.include(Extension.NAME, Extension.LOCATION, Extension.TYPE, Extension.STATUS, Extension.VERSION, Extension.CREATED, Extension.INSTALLDATE);
        field.include(Extension.NAME, Extension.CREATED);
        List<Cluster> list = extensionDAO.findPage(query, field, null, page);
        return list;
    }

    public Extension getDetail(String id) {
        Cluster cluster = extensionDAO.findById(id);
        return new Extension(cluster);
    }

    public int save(Extension extension) {
        extension.setId();
        extension.put(Extension.CREATED, new Date());
        extensionDAO.insert(extension);
        return ResponseCode.FINISH;
    }

    public long update(KeyUpdate keyUpdate) throws Exception {
        return extensionDAO.update(keyUpdate);
    }

    public int remove(List<String> ids) {
        Query query = new Query();
        query.idIn(ids);
        extensionDAO.remove(query);
        return ResponseCode.FINISH;
    }
}
